#include "edxrdreport.h"
#include <fstream>
#include "../logic.h"
#include "../model.h"
#include "../func.h"
#include "../data.h"
#include "../fit.h"


//Ftk *ftk = NULL;
using namespace std;

enum
{
    ID_EDXRD_reportquit = 25001,
	ID_EDXRD_browse				,
	ID_EDXRD_OK,
};


edxrdreport::edxrdreport(const wxString& title)
       : wxDialog(NULL, -1, title, wxPoint(-1, -1), wxSize(400, 200))
{

  wxPanel *panel = new wxPanel(this, -1);

  wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);

  wxBoxSizer *hbox1 = new wxBoxSizer(wxHORIZONTAL);
  wxStaticText *st1 =  new wxStaticText(panel, wxID_ANY, 
      wxT("EDXRD Report"));

  hbox1->Add(st1, 0, wxRIGHT, 8);
  
  vbox->Add(hbox1, 0, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, 10);

  vbox->Add(-1, 10);

  wxBoxSizer *hbox2 = new wxBoxSizer(wxHORIZONTAL);
	  tc = new wxTextCtrl(panel, wxID_ANY);
	  hbox2->Add(tc, 1, wxEXPAND| wxRIGHT, 5);
	  wxButton *btn_browse = new wxButton(panel, ID_EDXRD_browse, wxT("browse.."));
	  Connect( ID_EDXRD_browse, wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction) &edxrdreport::Onbrowse );
	  hbox2->Add(btn_browse);
  
  vbox->Add(hbox2, 0, wxLEFT | wxRIGHT| wxEXPAND, 10);

  vbox->Add(-1, 10);

  wxBoxSizer *hbox3 = new wxBoxSizer(wxVERTICAL);
	  cb1 = new wxCheckBox(panel, wxID_ANY, wxT("Include Error Report"));

	  hbox3->Add(cb1);
	  wxCheckBox *cb2 = new wxCheckBox(panel, wxID_ANY, 
		  wxT("Calculate Strains"));

	  hbox3->Add(cb2);

  vbox->Add(hbox3, 0, wxLEFT, 10);

  vbox->Add(-1, 25);

  wxBoxSizer *hbox4 = new wxBoxSizer(wxHORIZONTAL);
  wxButton *btn1 = new wxButton(panel, ID_EDXRD_OK, wxT("Ok"));
  Connect( ID_EDXRD_OK, wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction) &edxrdreport::OnOK );
  hbox4->Add(btn1, 0);
  wxButton *btn2 = new wxButton(panel, ID_EDXRD_reportquit, wxT("Close"));
  Connect( ID_EDXRD_reportquit, wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction) &edxrdreport::OnQuit );
  hbox4->Add(btn2, 0, wxLEFT | wxBOTTOM , 5);
  vbox->Add(hbox4, 0, wxALIGN_RIGHT | wxRIGHT, 10);

  panel->SetSizer(vbox);

  Centre();
}

void edxrdreport::OnQuit(wxCommandEvent&)
{
    Close(true);
}


void edxrdreport::Onbrowse(wxCommandEvent&)
{
	wxString name = "321321";
    	 wxString wildcards;
   	wildcards = "parameters of functions (*.csv)|*.csv";
	string export_dir_;
   	wxFileDialog fdlg(NULL, "Export curve to file", export_dir_, name,
                      wildcards + wxString("|all files|*"),
                      wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
   	if (fdlg.ShowModal() == wxID_OK) {
	wxString fileName = fdlg.GetPath();
	edxrdreport::tc->SetValue(fileName);
	}
	
	
	
}


void edxrdreport::OnOK(wxCommandEvent&)
{
	
	int func_size = ftk->functions().size();
    int data_size = ftk->get_dm_count();
	
    
			
    if(func_size>0)
	{
   	 
	ofstream file1;
	file1.open(tc->GetValue(), ios::out);
	
	Model const* filter_model = NULL;
	file1<<"#peak,file source,fit type, center, area,height,fwhm,error\n";
	//	file1<<ftk->get_model(0)->get_peak_parameters(errors);
		
	
	for(int j = 0; j< data_size; j++){
		 filter_model = ftk->get_model(j);

        	 for (int i = 0; i < func_size; ++i) 
			{
       			 if (filter_model && !contains_element(filter_model->get_ff().idx, i)
                           && !contains_element(filter_model->get_zz().idx, i))
           		 continue;
       			 Function const* f = ftk->get_function(i);
        			file1<<f->name<<",";
				file1<<ftk->get_data(j)->get_filename()<<",";
        			file1<<f->tp()->name<<",";
        			realt a;
       			 	file1<<(f->get_center(&a) ? S(a) : S("-"))<<",";
        			file1<<(f->get_area(&a)   ? S(a) : S("-"))<<",";
        			file1<<(f->get_height(&a) ? S(a) : S("-"))<<",";
        			file1<<(f->get_fwhm(&a)   ? S(a) : S("-"))<<",";
					
					
					
					DataAndModel* dm = const_cast<DataAndModel*>(ftk->get_dm(j));
					vector<DataAndModel*> dms(1, dm);
					vector<realt> errors = ftk->get_fit()->get_standard_errors(dms);
					VariableManager mgr(ftk);
					const SettingsMgr *sm = ftk->settings_mgr();
					

					for(int t=i*f->get_vars_count(); t< (i+1)*f->get_vars_count(); t++)
					{
					file1<<f->av()[t - i * f->get_vars_count()];
					file1<<" +/- "<<errors[t]<<";";
					
					}
					
					
					file1<<endl;
    		}

    	}
		file1.close();

    } //if(func_size>0)
	
	Close(true);
	
	
}